package com.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ServerConfigurationReader {
	private static String FILE_PATH="C:\\Documents and Settings\\picSEEK\\Desktop\\XMLRepository\\Service-config.xml";
	//private static String FILE_PATH="Service-config.xml";

public static boolean isDirect()throws Exception{
	NodeList nodeList=createDocument().getElementsByTagName("direct");
	Element element=(Element)nodeList.item(0);
	String proxyVal=element.getAttribute("IsUsed");
	if(proxyVal.equals("1"))
		return true;
	else 
		return false;
}


public static Map<String, String> getProxyDetails() throws Exception{
	Document doc=createDocument();
	NodeList nodeList=doc.getElementsByTagName("proxyaddress");
	Element element=(Element)nodeList.item(0);
	String proxyAddress=element.getChildNodes().item(0).getNodeValue().trim();
	element=(Element)doc.getElementsByTagName("proxyport").item(0);
	String proxyPort=element.getChildNodes().item(0).getNodeValue().trim();
	Map<String, String> proxyDetails=new HashMap<String, String>();
	proxyDetails.put("proxyaddress", proxyAddress);
	proxyDetails.put("proxyport", proxyPort);
	return proxyDetails;
}

public static String getClientConfigurationXMLPath()throws Exception{
	Document doc=createDocument();
	NodeList nodeList=doc.getElementsByTagName("clientConfigurationDetails");
	Element element=(Element)nodeList.item(0);
	String XMLPath=element.getChildNodes().item(0).getNodeValue().trim();
	return XMLPath;
}
private static Document createDocument()throws Exception{
	File file = new File(FILE_PATH);
	
	Document doc;
	//file = new File(path);
	DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
	//DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	DocumentBuilder db = dbf.newDocumentBuilder();
	doc = db.parse(file);
	db.reset();
	doc.getDocumentElement().normalize();

return doc;
}


public static void main(String[] args)throws Exception{
	Map<String, String> temp=getProxyDetails();
	System.out.println(getClientConfigurationXMLPath());
}
}
